package com.itbuka.goods.controller;

import com.itbuka.goods.service.GoodsProductService;
import com.itbuka.goods.domain.GoodsProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.itbuka.entity.Result;
import com.itbuka.entity.PageResult;
import com.github.pagehelper.Page;

import java.util.List;

/**
 * 商品表
 */
@RestController
@RequestMapping("/GoodsProduct")
public class GoodsProductController {

    @Autowired
    private GoodsProductService goodsProductService;

    /**
     * 查询全部商品表
     */
    @GetMapping("/selectAll")
    public Result<List<GoodsProduct>> selectAll() {
        return Result.ok("查询成功", goodsProductService.selectAll());
    }

    /**
     * 条件查询商品表
     */
    @PostMapping("/selectList")
    public Result<List<GoodsProduct>> select(@RequestBody GoodsProduct goodsProduct) {
        return Result.ok("条件查询成功", goodsProductService.selectList(goodsProduct));
    }

    /**
     * 新增商品表
     */
    @PostMapping("/insert")
    public Result<?> insert(@RequestBody GoodsProduct goodsProduct) {
        goodsProductService.insert(goodsProduct);
        return Result.ok("新增成功");
    }

    /**
     * 删除商品表
     */
    @GetMapping("/delete")
    public Result<?> delete(@RequestParam String ids) {
        goodsProductService.delete(ids);
        return Result.ok("删除成功");
    }

    /**
     * 修改商品表
     */
    @PostMapping("/update")
    public Result<?> update(@RequestBody GoodsProduct goodsProduct) {
        goodsProductService.update(goodsProduct);
        return Result.ok("修改成功");
    }

    /**
     * 分页搜索实现商品表
     */
    @GetMapping(value = "/pageAll")
    public Result<?> pageAll(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        Page<GoodsProduct> pageList = goodsProductService.pageAll(page, size);
        PageResult<?> pageResult = new PageResult<>(pageList.getTotal(), pageList.getResult());
        return Result.ok("查询成功", pageResult);
    }

    /**
     * 分页条件搜索实现商品表
     */
    @GetMapping(value = "/pageList")
    public Result<?> pageList(@RequestBody GoodsProduct goodsProduct, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        Page<GoodsProduct> pageList = goodsProductService.pageList(goodsProduct, page, size);
        PageResult<?> pageResult = new PageResult<>(pageList.getTotal(), pageList.getResult());
        return Result.ok("查询成功", pageResult);
    }

    /**
     * 审核
     */
    @PostMapping("/audit")
    public Result audit(@RequestParam Long id,@RequestParam Integer audit){
        Integer i = goodsProductService.audit(id,audit);
        if (i == -1) {
            return Result.fail("审核失败");
        }
        if (i == 2){
            return Result.fail("审核拒绝");
        }
        return Result.ok("审核成功");
    }

    /**
     * 上、下架
     */
    @PostMapping("/status")
    public Result<?> status(@RequestParam Long id, @RequestParam Integer status) {
        Integer i = goodsProductService.status(id, status);
        if (i != 1) {
            return Result.fail("上下架失败");
        }
        if (status == 0) {
            return Result.ok("下架成功");
        }
        return Result.ok("上架成功");
    }
}




